package com.smart_campus.sys.controller;

import com.smart_campus.sys.common.Constant;
import com.smart_campus.sys.common.DataGridView;
import com.smart_campus.sys.common.ResultObj;
import com.smart_campus.sys.domain.Menu;
import com.smart_campus.sys.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * @author Alfred
 * @date 2020/6/10 15:44
 */
@RestController
@RequestMapping("menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 查询全部菜单
     *
     * @param menu
     * @return
     */
    @RequestMapping("loadAllMenu")
    public Object loadAllMenu(Menu menu) {
        return this.menuService.queryAllMenu(menu);
    }
    /**
     * 查询菜单
     * @param
     * @return
     */
    @RequestMapping("loadMenu")
    public Object loadMenu(Menu menu){
        HashMap<String, Object> dataMap = new HashMap<>();
        dataMap.put("available",Constant.AVAILABLE_TRUE);
        dataMap.put("type",Constant.MENU_TYPE_MENU);
        List<Menu> menus = this.menuService.queryAllMenuForList(dataMap);
        return new DataGridView(Long.valueOf(menus.size()),menus);
    }

    /**
     * 查询最大排序码
     *
     * @return
     */
    @RequestMapping("queryMenuMaxOrderNum")
    public Object queryMenuMaxOrderNum() {
        Integer maxVaule = this.menuService.queryMenuMaxOrderNum();
        return new DataGridView(maxVaule + 1);
    }

    /**
     * 添加菜单和权限
     *
     * @param menu
     * @return
     */
    @RequestMapping("addMenu")
    public ResultObj addMenu(Menu menu) {
        try {
            if (menu.getType().equals("topmenu")) {
                menu.setPid(0);
            }
        //添加默认是展开
        menu.setSpread(Constant.SPREAD_TRUE);
        //可用类型
        menu.setAvailable(Constant.AVAILABLE_TRUE);
        this.menuService.insert(menu);
        return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_SUCCESS;
        }
    }

    @RequestMapping("updateMenu")
    public ResultObj updateMenu(Menu menu){
        try {
            this.menuService.updateByPrimaryKeySelective(menu);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }

    }

    @GetMapping("getMenuById")
    public Object getMenuById(Integer id){
        return new DataGridView( this.menuService.getById(id));
    }

    /**
     * 根据id查询当前菜单和权限的子菜单和权限的个数
     * @return Integer id
     */
    @GetMapping("getMenuChildrenCountById")
    public Object getMenuChildrenCountById(Integer id){
        Integer count = this.menuService.queryMenuChildrenCountById(id);
        return new DataGridView(count);
    }
    @RequestMapping("deleteMenu")
    public ResultObj deleteMenu(Integer id){
        try {
            this.menuService.deleteByPrimaryKey(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

}
